package lion.crawl;

/**
 * 抓取监控
 * 
 * @author Helin
 * @date 2012-8-19
 * @email hl_0074@sina.com
 */
public class TravelMonitor extends Thread {

	private Traveler travel;
	private boolean exit;

	public TravelMonitor(Traveler travel) {

		this.travel = travel;
	}

	@Override
	public void run() {

		Crawler[] crawlers = travel.getCrawlers();

		while (!exit) {

			boolean wait = false;
			for (Crawler crawler : crawlers) {
				wait = crawler.isWaiting();
				if (!wait) {
					break;
				}
			}

			// 当所有的都在等待并且没有新的url地址加入时，认为抓取完成
			if (wait && !travel.getUrlBase().hasNew()) {
				travel.stop();
			} else {
				try {
					sleep(500);
				} catch (InterruptedException e) {
					break;
				}
			}
		}
	}

	public void exit() {

		exit = true;
		interrupt();
	}
}
